<?php
/**
 * ad_info表model
 * 
 * @author sz 2016-9-23 10:34:43
 * @version 1.0
 */
namespace Home\Model;
use Think\Model;

class AdInfoModel extends Model
{

    /**
     * model对应表和数据库信息
     *
     * @author sz 2016-9-23 10:34:47
     * @version 1.0
     */
    function __construct ()
    {
        parent::__construct('ad_info', null, 'HAICHUANG');
    }

    /**
     * getAdInfo
     *
     * @return 查询用户数据
     * @author sz 2016-9-13 14:22:19
     * @version 1.0
     */
    public function getAdInfo ()
    {
        $sql = "SELECT a.id,a.user_id,a.title,a.dx_size,a.cc_size,a.weight,a.url,a.link,a.status,m.user_name FROM ad_info a
            LEFT JOIN member_user m ON m.id=a.user_id ORDER BY a.weight DESC";
        
        return $this->query($sql);
    }
    
    /**
     * getAdById
     *
     * @param $id
     *
     * @return 查询广告数据
     * @author sz 2016-9-23 10:41:35
     * @version 1.0
     */
    public function getAdById ($id)
    {
        $sql = "SELECT a.id,a.user_id,a.title,a.dx_size,a.cc_size,a.weight,a.url,a.link,m.user_name FROM ad_info a
            LEFT JOIN member_user m ON m.id=a.user_id WHERE a.id=" . $id;
        return $this->query($sql);
    }
    
    /**
     * updateStatus
     *
     * @param
     *
     * @return 删除成功true，失败返回false
     * @author sz 2016-9-23 10:42:14
     * @version 1.0
     */
    public function updateStatus ($id, $status)
    {
        $data['status'] = $status;
        return $this->where("id=$id")->save($data);
    }

    /**
     * add
     *
     * @param
     *
     * @return 添加成功true，失败返回false
     * @author sz 2015-12-9 15:09:25
     * @version 1.0
     */
    public function addAd($user_id, $title, $dx_size, $cc_size, $weight, $url, $link)
    {
        $dataArray = array();
        $dataArray['user_id'] = $user_id;
        $dataArray['title'] = $title;
        $dataArray['dx_size'] = $dx_size;
        $dataArray['cc_size'] = $cc_size;
        $dataArray['weight'] = $weight;
        $dataArray['link'] = $link;
         
        if($url != null)
        {
            $dataArray['url'] = $url;
        }
         
        $dataArray['create_time'] = array(
                'exp',
                'now()'
        );
        return $this->add($dataArray);
    }
    
    /**
     * modifyAd
     *
     * @param
     *
     * @return 修改成功true，失败返回false
     * @author sz 2016-9-23 10:44:26
     * @version 1.0
     */
    public function modifyAd($id, $user_id, $title, $dx_size, $cc_size, $weight, $url, $link)
    {
        $dataArray = array();
        $dataArray['user_id'] = $user_id;
        $dataArray['title'] = $title;
        $dataArray['dx_size'] = $dx_size;
        $dataArray['cc_size'] = $cc_size;
        $dataArray['weight'] = $weight;
        $dataArray['link'] = $link;
         
        if($url != null)
        {
            $dataArray['url'] = $url;
        }
         
        return $this->where("id=$id")->save($dataArray);
    }
    
    /**
     * del
     *
     * @param            
     *
     *
     * @return 删除成功true，失败返回false
     * @author sz 2016-9-23 10:45:06
     * @version 1.0
     */
    public function del ($id)
    {
        $where = array();
        $where['id'] = array('in', $id);
        return $this->where($where)->delete();
    }
}
?>